package com.force.abril.booking.download.transformer;

import static com.force.abril.booking.download.transformer.BookingFieldMapping.FIELDS;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtilsBean2;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import br.com.abril.booking.Booking;

import com.force.abril.transformer.Mapping;
import com.sforce.soap.partner.sobject.SObject;

@Component
public class BookingTransformer {

	private static final Logger logger = Logger.getLogger(BookingTransformer.class);
	
	public List<Booking> toBookingsFromSObject(SObject[] sfBookings) {
		List<Booking> bookings = new ArrayList<Booking>();
		for (SObject sfBooking : sfBookings) {
			bookings.add(toLocalBooking(sfBooking));
		}
		return bookings;
	}

	public Booking toLocalBooking(SObject salesForceRow) {

		Booking booking = new Booking();
		
		booking.setWallet( salesForceRow.getField("AgenciaMigrada__c").toString() +
						   salesForceRow.getField("AnuncianteMigrado__c").toString() +
						   salesForceRow.getField("Produto__c").toString() );
		
		for (Mapping mapping : FIELDS) {
			try {
				BeanUtilsBean2.getInstance().setProperty(booking,
						mapping.getKey(),
						salesForceRow.getField(mapping.getValue()));
			} catch (IllegalAccessException e) {
			} catch (InvocationTargetException e) {
			}
		}

		return booking;

	}

}
